﻿using AdAggregator.Advertisement;
using AdAggregator.Parser;
using AdAggregator.SearchData;

namespace AdAggregator.Website
{
    public abstract class BaseWebsite<TInSearchData, TOutSearchData, TAdvertisement, TParser> : IWebsite<TInSearchData, TAdvertisement>
        where TInSearchData : ISearchData
        where TOutSearchData : ISearchData
        where TAdvertisement : IAdvertisement
        where TParser : IParser<TOutSearchData, TAdvertisement>
    {
        protected TParser Parser;
        protected TOutSearchData SearchData;

        protected BaseWebsite(TParser parser)
        {
            Parser = parser;
        }

        protected abstract TOutSearchData MapSearchData(TInSearchData searchData);
        public IParseResult<TAdvertisement> Process(TInSearchData searchData)
        {
            SearchData = MapSearchData(searchData);
            SearchData.Page = searchData.Page;
            if (SearchData.IsSearchUnsupported)
            {
                return new ParseResult<TAdvertisement>();
            }
            return Parser.Parse(SearchData, this);
        }

        public TAdvertisement GetNewestVersionOfAd(string url)
        {
            return Parser.ParseSingleAd(url, this);
        }
    }
}